﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SuperQuery
{
    /// <summary>
    /// Interaction logic for ExportResults.xaml
    /// </summary>
    public partial class ExportResults : Window, Views.IExportResultsView
    {
        /// <summary>
        /// Initialise the ExportResults window
        /// </summary>
        /// <param name="dbListToExport">A list of databases containing results to export</param>
        public ExportResults(List<Models.Database> dbListToExport)
            : this()
        {
            // Set data context for data binding etc
            DataContext = this;

            m_Presenter = new Presenters.ExportResultsPresenter(this, dbListToExport);
        }

        /// <summary>
        /// Initialise the ExportResults window
        /// </summary>
        private ExportResults()
        {
            InitializeComponent();
        }

        #region Implementation of IExportResultsView

        /// <summary>
        /// The file to export the results to
        /// </summary>
        public string ExportFile
        {
            get
            {
                return (string)GetValue(ExportFileProperty);
            }
            set
            {
                SetValue(ExportFileProperty, value);
            }
        }

        /// <summary>
        /// The export style to use
        /// </summary>
        public Services.QueryExportStyle ExportStyle
        {
            get
            {
                return ConvertOptionButtonsToExportStyle();
            }
            set
            {
                ConvertExportStyleToOptionButtons(value);
            }
        }

        /// <summary>
        /// The type of exports (csv or tsv)
        /// </summary>
        public Services.QueryExportType ExportType
        {
            get
            {
                return ConvertOptionButtonsToExportType();
            }
            set
            {
                ConvertExportTypeToOptionButtons(value);
            }
        }

        /// <summary>
        /// The list of databases we with canCloseApplication sets that we can choose to export
        /// </summary>
        public List<Models.Database> AvailableDatabases
        {
            get
            {
                return (List<Models.Database>)GetValue(AvailableDatabasesProperty);
            }
            set
            {
                SetValue(AvailableDatabasesProperty, value);
            }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for AvailableDatabases.  This enables animation, styling, binding, etc...
        /// </summary>
        private static readonly DependencyProperty AvailableDatabasesProperty =
            DependencyProperty.Register("AvailableDatabases", typeof(List<Models.Database>), typeof(ExportResults), new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for ExportFile.  This enables animation, styling, binding, etc...
        /// </summary>
        private static readonly DependencyProperty ExportFileProperty =
            DependencyProperty.Register("ExportFile", typeof(string), typeof(ExportResults), new UIPropertyMetadata(null));

        #endregion

        /// <summary>
        /// Take an export style and select the correct option buttons from "Export Style" group
        /// </summary>
        /// <param name="exportStyle">The export style we are using to deterine which option button is selected</param>
        private void ConvertExportStyleToOptionButtons(Services.QueryExportStyle exportStyle)
        {
            switch (exportStyle)
            {
                case Services.QueryExportStyle.OneFileWithSeperators:
                {
                    optSameFileHeader.IsChecked = true;
                    break;
                }
                case Services.QueryExportStyle.OneFileContinuous:
                {
                    optSameFileNoHeader.IsChecked = true;
                    break;
                }
                case Services.QueryExportStyle.SeperateFilesWithDatabaseName:
                {
                    optSeperateFilesDatabaseName.IsChecked = true;
                    break;
                }
                default:
                {
                    // Any other value then set the option to export to seperate files
                    optSeperateFiles.IsChecked = true;
                    break;
                }
            }
        }

        /// <summary>
        /// Return the relevant export style based on which option button in the "Export Style" group is selected
        /// </summary>
        /// <returns>A QueryExportStyle specific to the selected option button</returns>
        private Services.QueryExportStyle ConvertOptionButtonsToExportStyle()
        {
            // By default set the export style to seperate files
            Services.QueryExportStyle result = Services.QueryExportStyle.SeperateFiles;

            if (optSameFileHeader.IsChecked.GetValueOrDefault(false))
            {
                result = Services.QueryExportStyle.OneFileWithSeperators;
            }
            else if (optSameFileNoHeader.IsChecked.GetValueOrDefault(false))
            {
                result = Services.QueryExportStyle.OneFileContinuous;
            }
            else if (optSeperateFilesDatabaseName.IsChecked.GetValueOrDefault(false))
            {
                result = Services.QueryExportStyle.SeperateFilesWithDatabaseName;
            }

            return result;
        }

        /// <summary>
        /// Take an export type and select the correct buttons in the "Export Format" group
        /// </summary>
        /// <param name="exportType">The export type we are using to determine which option button to select</param>
        private void ConvertExportTypeToOptionButtons(Services.QueryExportType exportType)
        {
            if (exportType == Services.QueryExportType.TabSeperated)
            {
                optTab.IsChecked = true;
            }
            else
            {
                optCSV.IsChecked = true;
            }
        }

        /// <summary>
        /// Return the relevant export type based on the option button selected in the "Export Format" group
        /// </summary>
        /// <returns>The export type specific to the select option button</returns>
        private Services.QueryExportType ConvertOptionButtonsToExportType()
        {
            // By default set the export type to comma seperated
            Services.QueryExportType result = Services.QueryExportType.CommaSeperated;

            if (optTab.IsChecked.GetValueOrDefault(false))
            {
                result = Services.QueryExportType.TabSeperated;
            }

            return result;
        }

        /// <summary>
        /// Show a "Save as" window for the user to select a file to export the data to
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.BrowseForExportFile();
        }

        /// <summary>
        /// Close the dialog without doing anything
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Export the data using the settings the user has provided, and if the export is successful, close the window
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnExportAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (m_Presenter.ExportData())
            {
                Close();
            }
        }

        /// <summary>
        /// Export the data using the settings the user has provided
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.ExportData();
        }

        /// <summary>
        /// User wants to select all items in the available canCloseApplication set list
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void bntSelectAll_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.SelectAll();
        }

        /// <summary>
        /// The user wants to un-select all items in the available canCloseApplication set list
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnSelectNone_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.SelectNone();
        }

        /// <summary>
        /// The presenter for this window
        /// </summary>
        Presenters.ExportResultsPresenter m_Presenter;
    }
}
